package net.entelijan.cobean.examples.showcase.bind;

import java.awt.Container;
import java.net.URL;
import java.util.Collection;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import net.entelijan.cobean.examples.showcase.bind.table.MasterDetailTablePanel;
import net.entelijan.cobean.examples.showcase.bind.table.TablePanel;
import net.entelijan.scf.InitialisationException;
import net.entelijan.scf.ShowcaseDescriptor;
import net.miginfocom.swing.MigLayout;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Convenience class for testing a single showcase.
 * 
 * To switch the showcase that should be tested change the inner Panel of the
 * ShowcasePanel and the appropriate setter and getter.
 * 
 */
public class SingleBindShowcaseRunner {

	private static Log log = LogFactory.getLog(SingleBindShowcaseRunner.class);

	public SingleBindShowcaseRunner() {
		super();
	}

	public static void main(String[] args) {
		ShowcasePanel topPanel = new ShowcasePanel();
		final JFrame frame = createFrame(topPanel);
		initShowcasePanel(topPanel);
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				frame.setVisible(true);
			}

		});
	}

	private static JFrame createFrame(ShowcasePanel topPanel) {
		final JFrame frame = new JFrame("Single Bind Showcase");
		URL url = SingleBindShowcaseRunner.class.getClassLoader().getResource(
				"logo-small-white.png");
		if (url != null) {
			frame.setIconImage(new ImageIcon(url).getImage());
		}
		Container cp = frame.getContentPane();
		cp.setLayout(new MigLayout());
		cp.add(topPanel);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800, 800);
		frame.setLocation(100, 200);
		return frame;
	}

	private static void initShowcasePanel(ShowcasePanel topPanel) {
		for (ShowcaseDescriptor d : loadShowcaseDescs()) {
			try {
				d.init(topPanel);
			} catch (InitialisationException e) {
				// Ignore initialisation exceptions.
				log.debug("Could not initialize '" + d + "'");
			} catch (Exception e) {
				log.error("Could not initialize '" + d + "'", e);
			}
		}
	}

	@SuppressWarnings("unchecked")
	private static Collection<ShowcaseDescriptor> loadShowcaseDescs() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"bind-showcase.xml");
		return (Collection<ShowcaseDescriptor>) ctx.getBeansOfType(
				ShowcaseDescriptor.class).values();
	}

	@SuppressWarnings("unused")
	private static class ShowcasePanel extends JPanel {

		// Change this panel and the appropriate setter and getters to test
		// another showcase
		private MasterDetailTablePanel masterDetailTablePanel = new MasterDetailTablePanel();
		private TablePanel tablePanel = new TablePanel();

		private static final long serialVersionUID = 1L;

		public ShowcasePanel() {
			super();
			setLayout(new MigLayout());
			add(tablePanel, "w max, h max");
		}

		public MasterDetailTablePanel getMasterDetailTablePanel() {
			return masterDetailTablePanel;
		}

		public void setMasterDetailTablePanel(
				MasterDetailTablePanel masterDetailTablePanel) {
			this.masterDetailTablePanel = masterDetailTablePanel;
		}

		public TablePanel getTablePanel() {
			return tablePanel;
		}

		public void setTablePanel(TablePanel tablePanel) {
			this.tablePanel = tablePanel;
		}

	}

}
